package com.atqh.logistics.service.impl;


import com.atqh.logistics.domin.Mission;
import com.atqh.logistics.mapper.MissionMapper;
import com.atqh.logistics.utils.ErrorCode;
import com.atqh.logistics.utils.Result;
import com.atqh.logistics.utils.Vo.params.CustomRegisParam;
import com.atqh.logistics.utils.Vo.params.MissionParam;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atqh.logistics.domin.Custom;
import com.atqh.logistics.service.CustomService;
import com.atqh.logistics.mapper.CustomMapper;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 *
 */
@Service
public class CustomServiceImpl extends ServiceImpl<CustomMapper, Custom>
        implements CustomService {
    private static final String salt = "lum!@#";

    @Autowired
    private CustomMapper customMapper;
    @Autowired
    private MissionMapper missionMapper;


    @Override
    public Result login(String account, String password, HttpServletRequest req) {

        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        String psw = DigestUtils.md5Hex(password + salt);
        LambdaQueryWrapper<Custom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Custom::getAccount, account).eq(Custom::getPassword, psw).last("limit 1");
        Custom custom = customMapper.selectOne(queryWrapper);
        if (custom == null) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        } else if (custom != null) {
            req.getSession().setAttribute("custom", custom);
        }
        return Result.success(custom);
    }

    @Override
    public Result logout(HttpServletRequest req) {

        Custom custom = (Custom) req.getSession().getAttribute("custom");
        if (custom != null) req.getSession().invalidate();
        return Result.success(null);

    }

    @Override
    public Result register(CustomRegisParam customRegisParam) {
        String account = customRegisParam.getAccount();
        String password = customRegisParam.getPassword();
        if (StringUtils.isBlank(account) || StringUtils.isBlank(password)) {
            return Result.Fail(ErrorCode.PARAMS_ERROR.getCode(), ErrorCode.PARAMS_ERROR.getMsg());
        }

        LambdaQueryWrapper<Custom> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Custom::getAccount, account);
        Custom custom = customMapper.selectOne(queryWrapper);

        if (custom != null) {
            return Result.Fail(ErrorCode.ACCOUNT_EXIST.getCode(), "账号已经被用了欧");
        }
        Custom custom1 = new Custom();
        custom1.setAccount(account).setName(customRegisParam.getName()).setPhoneNum(customRegisParam.getPhoneNum());
        custom1.setPassword(DigestUtils.md5Hex(password + salt));
        customMapper.insert(custom1);
        return Result.success(null);
    }

    @Override
    public Result PublicMission(MissionParam missionParam, HttpServletRequest req) {
        Mission mission = new Mission();
        BeanUtils.copyProperties(missionParam, mission);
        Custom custom = (Custom) req.getSession().getAttribute("custom");
        mission.setCustomId(custom.getId()).setIsDeleted(0);
        missionMapper.insert(mission);
        return Result.success(null);
    }

    @Override
    public Result ViewPublicMission(HttpServletRequest req) {

        LambdaQueryWrapper<Mission> queryWrapper = new LambdaQueryWrapper<>();
        Custom custom = (Custom) req.getSession().getAttribute("custom");
        queryWrapper.eq(Mission::getCustomId, custom.getId());
        List<Mission> missions = missionMapper.selectList(queryWrapper);
        return Result.success(missions);
    }

    @Override
    public Result ViewNowIngMission(HttpServletRequest req) {
        LambdaQueryWrapper<Mission> queryWrapper = new LambdaQueryWrapper<>();
        Custom custom = (Custom) req.getSession().getAttribute("custom");
        queryWrapper.eq(Mission::getFlag, 1).eq(Mission::getCustomId, custom.getId());
        List<Mission> missions = missionMapper.selectList(queryWrapper);
        return Result.success(missions);

    }

    @Override
    public Result getOwnInfo(HttpServletRequest req) {

        LambdaQueryWrapper<Custom> queryWrapper = new LambdaQueryWrapper<>();
        Custom custom1 = (Custom) req.getSession().getAttribute("custom");
        queryWrapper.eq(Custom::getId, custom1.getId());
        Custom custom = customMapper.selectOne(queryWrapper);
        if (custom == null) {
            return Result.Fail(ErrorCode.ACCOUNT_PWD_NOT_EXIST.getCode(), ErrorCode.ACCOUNT_PWD_NOT_EXIST.getMsg());
        }
        return Result.success(custom);
    }
}




